package write;

import java.awt.Window;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadWithKeyboard {

	public void openKeyboard() throws IOException {

		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(sr);

		System.out.println("Input Word!");

		String word = br.readLine();

		if (word == null || word.trim().equals("")) {
			openKeyboard();
			return;
		}

		if (word.equals("quit")) {
			System.out.println("Thanks, See you.");
			return;
		}

		if (word.equals("save")) {
			writeToFile(word);
		}

		openKeyboard();
	}

	public void writeToFile(String word) {

		try {

			System.out
					.println("=====================================================================");

			// String path = this.getClass().getResource("/").getPath();
			String path = "C:\\";
			String fileName = "savedWord.txt";
			System.out.println("path + fileName : " + path + fileName);

			File file = new File(path + fileName);
			System.out.println("file exist : " + file.exists());
			System.out.println("file path : " + file.getAbsolutePath());

			System.out
					.println("=====================================================================");

			BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
			bw.append(word);
			bw.newLine();
			System.out.println("We saved your word : " + word);
			bw.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}
}
